package org.googlecode.lightning.search.index;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import org.apache.lucene.document.DateTools;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.daragon.fulltext.parse.ParseTool;

/**
 * This is the document
 * 
 * @author daragon
 * 
 */
public class IndexDocument implements IDocument {

	private ParseTool parser;

	public void setParseTool(ParseTool parser) {
		this.parser = parser;
	}

	/**
	 * add file's information to the indexWriter
	 * 
	 * @param writer
	 * @param file
	 * @throws IOException
	 * @throws CorruptIndexException
	 * @throws IOException
	 */
	public void document(IndexWriter writer, File file)
			throws CorruptIndexException, IOException {
		Document doc = new Document();
		String path = file.getCanonicalPath();
		String text = parser.handle(path);

		if (text != null) {
			IndexField.FILE_NAME.addToDoc(doc, path);
			IndexField.CONTENT.addToDoc(doc, text);
			IndexField.LAST_MODIFIED.addToDoc(doc, DateTools.timeToString(file
					.lastModified(), DateTools.Resolution.MINUTE));
			writer.addDocument(doc);
		}
	}

	/**
	 * @throws IOException
	 * @throws CorruptIndexException
	 * 
	 */
	public void document(IndexWriter writer, String path)
			throws CorruptIndexException, IOException {
		this.document(writer, new File(path));
	}

	/**
	 * @throws IOException
	 * @throws CorruptIndexException
	 * @throws IOException
	 * @throws CorruptIndexException
	 * 
	 */
	public void document(IndexWriter writer, Serializable id, String filename,
			byte[] content) throws CorruptIndexException, IOException {
		Document doc = new Document();
		String text = parser.handle(id, filename, content);

		if (text != null) {
			IndexField.ID.addToDoc(doc, id + "");
			IndexField.FILE_NAME.addToDoc(doc, filename);
			IndexField.CONTENT.addToDoc(doc, text);
			writer.addDocument(doc);
		}
	}

	/**
	 * @throws IOException
	 * @throws CorruptIndexException
	 * 
	 */
	public void document(IndexWriter writer, Serializable id, String text)
			throws CorruptIndexException, IOException {
		if (text != null ) {
			Document doc = new Document();
			IndexField.ID.addToDoc(doc, id + "");
			IndexField.CONTENT.addToDoc(doc, text);
			writer.addDocument(doc);
		}
	}

	/**
	 * 
	 * @param indexWriter
	 * @param id
	 * @param file
	 * @throws IOException
	 */
	public void document(IndexWriter writer, Serializable id, File file)
			throws IOException {
		Document doc = new Document();
		String path = file.getCanonicalPath();
		String text = parser.handle(path);
		if (text != null) {
			IndexField.ID.addToDoc(doc, id + "");
			IndexField.FILE_NAME.addToDoc(doc, path);
			IndexField.CONTENT.addToDoc(doc, text);
			IndexField.LAST_MODIFIED.addToDoc(doc, DateTools.timeToString(file
					.lastModified(), DateTools.Resolution.MINUTE));
			writer.addDocument(doc);
		}
	}
	
	
}
